package com.carhailing.system.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.carhailing.common.domain.CouponUseRecord;
import com.carhailing.common.result.Result;
import com.carhailing.system.service.ICouponUseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户使用优惠券记录表
 *
 * @author zct
 * @since 2024-07-23
 */
@RestController
@RequestMapping("/couponUseRecord")
public class CouponUseRecordController {
   @Autowired
   private ICouponUseRecordService iCouponUseRecordService;

   /**
    * 查询列表
    *
    * @param couponUseRecord
    */
   @PostMapping("getList")
   public Result<List<CouponUseRecord>> getList(@RequestBody CouponUseRecord couponUseRecord) {
      List<CouponUseRecord> list1 = iCouponUseRecordService.list();
      for (CouponUseRecord couponUseRecord1 : list1) {
         System.out.println(couponUseRecord1);
      }
      return Result.success(list1);
   }

   /**
    * 添加
    *
    * @param couponUseRecord
    * @return
    */
   @PostMapping("/add")
   public Result<Boolean> add(@RequestBody CouponUseRecord couponUseRecord) {
      boolean b = iCouponUseRecordService.save(couponUseRecord);
      return Result.success(b);
   }

   /**
    * 删除
    *
    * @param couponUseRecord
    * @return
    */
   @PostMapping("/del")
   public Result<Boolean> del(@RequestBody CouponUseRecord couponUseRecord) {
      couponUseRecord.setRecordId(1L);
      boolean b = iCouponUseRecordService.removeById(couponUseRecord);
      return Result.success(b);
   }


   @SentinelResource("HelloWorld")
   public void helloWorld() {
      // 资源中的逻辑
      System.out.println("hello world");
   }

   @GetMapping("Test1")
   public void Test1() {
      // 调用 配置规则.
      initFlowRules();
      // 1.5.0 版本开始可以直接利用 try-with-resources 特性
      try (Entry entry = SphU.entry("HelloWorld")) {
         // 被保护的逻辑
         helloWorld();
//         System.out.println("hello world");
      } catch (BlockException ex) {
         // 处理被流控的逻辑
         System.out.println("blocked!");
      }
   }

   /**
    * 配置规则
    */
   private static void initFlowRules() {
      List<FlowRule> rules = new ArrayList<>();
      FlowRule rule = new FlowRule();
      rule.setResource("HelloWorld");
      rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
      // Set limit QPS to 20.
      rule.setCount(1);
      rules.add(rule);
      FlowRuleManager.loadRules(rules);
   }
}